import axios from 'axios'
import Cookies from 'js-cookie'

axios.defaults.baseURL = 'https://nongyun.hz-hyf.com/'

//响应拦截器
axios.interceptors
    .response
    .use((response) => {
        // console.log(response.data);
        // console.log("message",response.data.message)
        //统一处理响应数据
        if (response.data.message == "login.noLogin") {
            Cookies.remove('user');
            Cookies.remove('password');
            Cookies.remove('hasGreet');
            Cookies.remove('access');
            Cookies.remove('access_token')
            // parent.location.href ='login'
            // vm.$router.push('/login')
            return;
        } else if (response.data.error_code != 0) {
            alert(response.data.message)
            return;
        }
        return response.data.data;
    }, (error) => {
        alert(error);
    });


//注册账号
export const register = params => {
    var url = '/api/v1/auth/register';
    return axios.post(url, params);
}


//登陆
export const login = parmas => {
    var url = 'api/v1/auth/login';
    return axios.post(url, parmas);
    // .then(res => check(res.data))
    // .catch(err => {
    //     alert(err)
    // })
}


//获取节点数据
export const getStationData = params => {
    var url = 'api/v1/caipos/data_table';
    return axios.get(url, {params: params});
}

// https://nongyun.hz-hyf.com/api/v1/crops/categories?access_token=a29a980ea478adfdded74da5824690dc
//获取作物分类级联数据
export const getCropClassify = params => {
    var url = 'api/v1/crops/categories';
    return axios.get(url, {params: params})
}

//获取肥料分类
export const getSoilClassify = params => {
    var url = 'api/v1/ferts/categories';
    return axios.get(url, {params: params})
}
